const Evt = require("./event.js")
const {EN} = require("./constant.js")

module.exports = class ImagePicker extends Evt{
	constructor($env,$event){
		super()
		this.$event = $event;
		this.active = true;
		this.bind()
	}
	bind(){
		document.addEventListener("click",(e)=>{
			if(!this.active)return true;
			e.stopPropagation()
			e.preventDefault()
			e.stopImmediatePropagation()
			let t = e.target;
			if(t.tagName==="IMG"){
				this.afterPick(this.active)
				this.$event.emit(EN.img_done,t)
			}
		})
	}
	toggleDisabled(disable){
		[].forEach.call(document.querySelectorAll("a"),(a)=>{
			if(disable){
				a.setAttribute("data-drawhref",a.href)
				a.href="javascript:void(0)"
			}else{;
				a.href=a.dataset.drawhref
			}
			
		})
	}
	afterPick(done){
		document.body.classList.remove("img-picking")
		this.toggleDisabled(false)
		this.active = false;
	}
	show(){
		document.body.classList.add("img-picking")
		this.active = true;
		this.toggleDisabled(true)
		this.$event.emit(EN.img)
	}
}